﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace AchievementBackup
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread.Sleep(10000);
            if (args.Length == 2)
            {
                string srcDir = args[0];
                string tgtDir = args[1];
                CopyDirectory(srcDir, tgtDir);
            }
        }

       

        /// <summary>
        /// 注意的地方是需要检查一定不能让父目录拷贝到子目录中，否则程序会一直递归执行下去直到出错
        /// </summary>
        /// <param name="srcDir"></param>
        /// <param name="tgtDir"></param>
        static void CopyDirectory(string srcDir, string tgtDir)
        {
            DirectoryInfo source = new DirectoryInfo(srcDir);
            DirectoryInfo target = new DirectoryInfo(tgtDir);

            if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
            {
                throw new Exception("父目录不能拷贝到子目录！");
            }

            if (!source.Exists)
            {
                return;
            }

            if (!target.Exists)
            {
                target.Create();
            }

            FileInfo[] files = source.GetFiles();

            for (int i = 0; i < files.Length; i++)
            {
                File.Copy(files[i].FullName, target.FullName + @"\" + files[i].Name, true);
            }

            DirectoryInfo[] dirs = source.GetDirectories();

            for (int j = 0; j < dirs.Length; j++)
            {
                CopyDirectory(dirs[j].FullName, target.FullName + @"\" + dirs[j].Name);
            }
        }
    }
}
